﻿using System;
using System.Configuration;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace mojoPortal.Web.RS.SBM.Controls
{
    public partial class EmployeeListControl : RSBaseUserControl
    {

        
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void lnkManagePermission_Click(object sender, CommandEventArgs e)
        {
            int employeeid = Convert.ToInt32(e.CommandArgument.ToString());
            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (userIdentity.RSCanWrite)
                ucModalManagePermissionControl.ShowModalManagePermission(userIdentity.RSCompanyId, employeeid);
            else
                ucModalActionMessageControl.ShowModal("You Do Not Have Permission To Perform This Action!", "", false);
        }

        protected void gvEmployee_Edit(object sender, CommandEventArgs e)
        {
            int employeeid = Convert.ToInt32(e.CommandArgument.ToString());
            if (employeeid > 0)
                Response.Redirect("RSManageEmployee.aspx?employeeid=" + employeeid);
        }
        protected void btnAddNewEmployee_Click(object sender, EventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            if(userIdentity.RSCanWrite)
                Response.Redirect("~/RS/SBM/RSManageEmployee.aspx?action=add");
            else
                ucModalActionMessageControl.ShowModal("You Do Not Have Permission To Perform This Action!", "", false);
        }
        public bool LoadEmployeeDataList(int companyid)
        {
            bool bHasValue = true;

            SqlCommand myCommand;
            SqlParameter param;
            SqlConnection myConnection;

            String sqlSELECT = "SELECT EmployeeId, LastName, FirstName, Email, HomePhone, CellPhone, ReportsTo FROM [rs_tblEmployee] WHERE CompanyId=@CompanyId ORDER BY LastName, FirstName";

            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(sqlSELECT, myConnection);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@companyid";
            param.Value = companyid;
            myCommand.Parameters.Add(param);

            SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
            myConnection.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            
            gvEmployeeList.DataSource = ds;
            gvEmployeeList.DataBind();
            Session["DATATABLE"] = ds.Tables[0];
            myConnection.Close();

            if (gvEmployeeList.Rows.Count == 0)
            {
                bHasValue = false;
                return bHasValue;
            }
            return bHasValue;
        }
        private string ConvertSortDirectionToSql(SortDirection sortDirection)
        {
            string newSortDirection = String.Empty;

            switch (sortDirection)
            {
                case SortDirection.Ascending:
                    newSortDirection = "ASC";
                    break;

                case SortDirection.Descending:
                    newSortDirection = "DESC";
                    break;
            }
            return newSortDirection;
        }

        protected void gvEmployeeList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvEmployeeList.PageIndex = e.NewPageIndex;
            gvEmployeeList.DataBind();
        }

        protected void gvEmployeeList_Sorting(object sender, GridViewSortEventArgs e)
        {
            DataTable dataTable = Session["DATATABLE"] as DataTable;
            if (dataTable != null)
            {
                DataView dataView = new DataView(dataTable);
                dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

                gvEmployeeList.DataSource = dataView;
                gvEmployeeList.DataBind();
            }
        }

    }
}